const Flatten = require("../template")

function testFlattenAdvancedMethods() {
  console.log(
    "1. 带过滤的扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5], [6, 7]]
      const result = Flatten.flattenWithFilter(arr, (item) => item % 2 === 0, 1)
      const expected = [2, 4, 6]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 带映射的扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flattenWithMap(arr, (item) => item * 2, 1)
      const expected = [2, 4, 6, 8, 10]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 带去重的扁平化测试:",
    (() => {
      const arr = [1, [2, 2], [3, 3, 3]]
      const result = Flatten.flattenWithUnique(arr, 1)
      const expected = [1, 2, 3]
      return JSON.stringify(result.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "4. 带排序的扁平化测试:",
    (() => {
      const arr = [3, [1, 4], [2, 5]]
      const result = Flatten.flattenWithSort(arr, (a, b) => a - b, 1)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 带分组的扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flattenWithGroup(arr, (item) => item % 2 === 0 ? 'even' : 'odd', 1)
      return result.even.length === 2 && result.odd.length === 3
    })()
  )

  console.log(
    "6. 带统计的扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flattenWithStats(arr, 1)
      return result.length === 5 && result.originalLength === 3
    })()
  )

  console.log(
    "7. 异步扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const processor = async (item) => item * 2
      
      return new Promise((resolve) => {
        Flatten.flattenAsync(arr, processor, 1).then((result) => {
          const expected = [2, 4, 6, 8, 10]
          resolve(JSON.stringify(result) === JSON.stringify(expected))
        })
      })
    })()
  )

  console.log(
    "8. 并发扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5], [6, 7]]
      const processor = async (item) => item * 2
      
      return new Promise((resolve) => {
        Flatten.flattenConcurrent(arr, processor, 1, 2).then((result) => {
          const expected = [2, 4, 6, 8, 10, 12, 14]
          resolve(JSON.stringify(result) === JSON.stringify(expected))
        })
      })
    })()
  )

  console.log(
    "9. 复杂对象扁平化测试:",
    (() => {
      const arr = [
        { id: 1, name: 'A', items: [1, 2] },
        [{ id: 2, name: 'B', items: [3, 4] }, { id: 3, name: 'C', items: [5, 6] }]
      ]
      const result = Flatten.flattenWithMap(arr, (item) => ({
        ...item,
        processed: true
      }), 1)
      
      return result.length === 3 &&
             result.every(item => item.processed === true) &&
             result[0].id === 1
    })()
  )

  console.log(
    "10. 多维数组扁平化测试:",
    (() => {
      const arr = [
        [1, [2, 3]],
        [[4, 5], [6, 7]],
        [8, [9, 10]]
      ]
      const result = Flatten.flattenDeep(arr)
      const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "11. this上下文高级方法测试:",
    (() => {
      const advancedObj = {
        multiplier: 2,
        processAdvanced: function(arr) {
          const flattened = Flatten.flattenWithMap(arr, (item) => item * this.multiplier, 1)
          return flattened.length
        }
      }
      const arr = [1, [2, 3], [4, 5]]
      return advancedObj.processAdvanced(arr) === 5
    })()
  )

  console.log(
    "12. 复杂对象this绑定高级方法测试:",
    (() => {
      const advancedConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(arr) {
          const flattened = Flatten.flattenWithFilter(arr, (item) => item >= this.config.threshold, 1)
          return flattened.length > 0
        }
      }
      const arr = [1, [2, 3], [4, 5]]
      return advancedConfigObj.processWithConfig(arr) === true
    })()
  )
}

testFlattenAdvancedMethods()
